package com.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flower.domain.dto.buyer.cart.CartDto;
import com.flower.domain.dto.buyer.cart.CartQueryDto;
import com.flower.domain.entity.Cart;
import com.flower.domain.entity.Product;
import com.flower.domain.vo.buyer.CartVO;
import com.flower.service.CartService;
import com.flower.mapper.CartMapper;
import com.flower.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86178
* @description 针对表【cart(购物车表)】的数据库操作Service实现
* @createDate 2025-03-04 12:26:59
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements CartService{

    @Autowired
    private ProductService productService;

    @Override
    public Page<CartVO> getCartList(CartQueryDto queryDto) {
        Page<Cart> page = new Page<>(queryDto.getPageNumber(), queryDto.getPageSize());
        
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getBuyerId, queryDto.getBuyerId());
        
        Page<Cart> cartPage = page(page, wrapper);
        
        // 转换为VO
        Page<CartVO> voPage = new Page<>();
        BeanUtils.copyProperties(cartPage, voPage, "records");
        
        List<CartVO> voList = cartPage.getRecords().stream().map(cart -> {
            CartVO vo = new CartVO();
            BeanUtils.copyProperties(cart, vo);
            
            // 获取商品信息
            Product product = productService.getById(cart.getProductId());
            if (product != null) {
                vo.setName(product.getProductName());
                vo.setImage(product.getImage());
                vo.setPrice(product.getPrice());
                vo.setShopId(product.getShopId());
            }
            
            return vo;
        }).filter(vo -> {
            if (StringUtils.hasText(queryDto.getProductName())) {
                return vo.getName().contains(queryDto.getProductName());
            }
            return true;
        }).collect(Collectors.toList());
        
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public void addToCart(CartDto cartDto) {
        // 检查是否已存在
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getBuyerId, cartDto.getBuyerId())
                .eq(Cart::getProductId, cartDto.getProductId());
        Cart existCart = getOne(wrapper);
        
        if (existCart != null) {
            // 已存在则更新数量
            existCart.setQuantity(existCart.getQuantity() + cartDto.getQuantity());
            updateById(existCart);
        } else {
            // 不存在则新增
            Cart cart = new Cart();
            cart.setBuyerId(cartDto.getBuyerId());
            cart.setProductId(cartDto.getProductId());
            cart.setQuantity(cartDto.getQuantity());
            cart.setAddTime(new Date());
            save(cart);
        }
    }

    @Override
    public void updateCart(CartDto cartDto) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getBuyerId, cartDto.getBuyerId())
                .eq(Cart::getProductId, cartDto.getProductId());
        Cart cart = getOne(wrapper);
        if (cart != null) {
            cart.setQuantity(cartDto.getQuantity());
            updateById(cart);
        }
    }

    @Override
    public void clearCart(Long buyerId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getBuyerId, buyerId);
        remove(wrapper);
    }
}




